/* 
    file:   ui_core.h
 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __UI_CORE_H
#define __UI_CORE_H

#ifdef __cplusplus
 extern "C" {
#endif
/* ------------------------------------------------------------------------- */

#include "stdint.h"

#include "ui_dev.h"

/* ------------------------------------------------------------------------- */
#define     RGB888T565(r,g,b)           (((0xf8&r)<<8)|((0xfc&g)<<3)|(0xf8&b>>3))
/* ------------------------------------------------------------------------- */

//struct _ui_core;
//void LCD_Color_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 *color);				//填充指定颜色
typedef void (*scr_print)(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t *color);
typedef struct ui_screen
{
    uint32_t            width;
    uint32_t            heigh;
    void            *   pbfb;//背景缓冲
    void            *   pbfa;//动作叠加缓冲
    scr_print           printer;//向屏幕输送图片
}ui_screen_t;
/* ------------------------------------------------------------------------- */

typedef struct 
{
    int x;
    int y;
}point;
typedef unsigned int (*get_touch_cb)(point *ppnt);
typedef struct ui_input
{
    get_touch_cb        pgt_cb;
}ui_input_t;
typedef struct ui_core
{
    struct ui_screen            *   pscr;//屏幕信息
    struct ui_input             *   pindev;//输入信息
    uint8_t                         status;
    //时间信息
}ui_core;

/* ------------------------------------------------------------------------- */

typedef struct
{
    uint8_t glass : 1; /**< 1: Do not inherit this style*/

    /** Object background. */
    struct
    {
        uint16_t main_color; /**< Object's main background color. */
        uint16_t grad_color; /**< Second color. If not equal to `main_color` a gradient will be drawn for the background. */
        uint16_t radius; /**< Object's corner radius. You can use #LV_RADIUS_CIRCLE if you want to draw a circle. */
        uint8_t opa; /**< Object's opacity (0-255). */

    } body;

    /** Style for text drawn by this object. */
    struct
    {
        uint16_t color; /**< Text color */
        uint16_t sel_color; /**< Text selection background color. */
        const void * font;
        uint16_t letter_space; /**< Space between letters */
        uint16_t line_space; /**< Space between lines (vertical) */
        uint8_t opa; /**< Text opacity */
    } text;
} ui_style_t;
/* ------------------------------------------------------------------------- */

struct ui_obj;

typedef struct
{
    int     x1;
    int     y1;
    int     x2;
    int     y2;
}ui_area;

/** Signals are for use by the object itself or to extend the object's functionality.
  * Applications should use ::lv_obj_set_event_cb to be notified of events that occur
  * on the object. */
// enum {
//     /*Input device related*/
//     LV_SIGNAL_PRESSED,           /**< The object has been pressed*/
//     LV_SIGNAL_PRESSING,          /**< The object is being pressed (called continuously while pressing)*/
//     LV_SIGNAL_RELEASED,          /**< User pressed object for a short period of time, then released it. Not called if dragged. */
// };
enum{
    ui_signal_none,
    ui_signal_init,
    ui_signal_init_r,
    ui_signal_pressed,
    ui_signal_pressing,
    ui_signal_releassed,
};


typedef void (*ui_event_cb_t)(struct ui_obj * obj, uint8_t event);
typedef uint8_t (*ui_signal_cb_t)(struct ui_obj * obj, uint8_t sign, void * param);//绘制只有这个地方需要检查
typedef uint8_t (*ui_design_cb_t)(struct ui_obj * obj, uint8_t mode);//
typedef struct ui_obj
{
    struct ui_obj       *   par;

    struct ui_obj       *   item;           //link       children objs[AM,AD]
    struct ui_obj       *   pre;            //link
    struct ui_obj       *   next;           //link

    ui_area                 area;           //相对与par的位置


    void                *   objdat;         //项目用数据[AM,AD] 

    const ui_style_t    *   style_p;        // Pointer to the object's style
    
    ui_event_cb_t           event_cb;       // user set
    ui_signal_cb_t          signal_cb;      // 
    ui_design_cb_t          design_cb;      // 

    uint8_t                 status;         //0未绘制 1已经绘制 2有动作
    uint8_t                 touch;
    uint8_t                 hiden;          // 1 hide   

    void                *   pudata;
}ui_obj_t;
/* ------------------------------------------------------------------------- */
int ui_int_limit(int din,int num1,int num2);
uint16_t    AlphaColor565(uint16_t cla,uint16_t clb,uint8_t alpha);
void ui_memfill(void * pdat,unsigned char dc,uint32_t len);


struct ui_obj * ui_newobj(struct ui_obj * ppar);
void ui_objseteventcb(struct ui_obj *objs,ui_event_cb_t eventcb);
void ui_getlct(struct ui_obj *objs,  point * ppnt);
void ui_delobj(struct ui_obj * ptg);
void ui_run(struct ui_core * pcore,struct ui_obj *objs);
uint8_t ui_objreflash(struct ui_obj *objs,struct ui_core * param);
//void ui_lctreflash(struct ui_obj *objs);

void ui_obj_set_pos(struct ui_obj * pobj,uint32_t px,uint32_t py);

/* ------------------------------------------------------------------------- */
#ifdef __cplusplus
}
#endif

#endif /* __UI_CORE_H */
